<?php

namespace App\Http\Controllers\Shop;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ShopController extends Controller
{
    /**
     * 店铺注册页面
     */
    public function getRegister()
    {
        return view('Shop.register');
    }

    /**
     * 店铺首页
     * @return \Illu minate\Http\Response
     */
    public function getIndex(Request $request)
    {
        //读取店铺的id
        $sid = session()->get('sname')['id'];   
        
        $nick = DB::table('shops_details')->where('id','=',$sid)->value('nickname');
        return view('Shop.index',['nick'=>$nick,'sid'=>$sid]);
    }

    /**
     * 店铺的配置
     * @return \Illuminate\Http\Response
     */
    public function getSetting()
    {
        $id = session()->get('sname')['id'];
        //原来店铺状态
        $data = DB::table('shops_details')->join('shops','shops.id','=','shops_details.id')->where('shops_details.id','=',$id)->get()[0];
        //显示页面
        return view('shop.setting',['sid'=>$id,'data'=>$data]);
    }

    /**
     * 执行店铺修改
     * @return \Illuminate\Http\Response
     */
    public function postDusetting(Request $request)
    {
        $data = $request->except(['status','_token']);
        $id = $data['id'];
        //获取原来店铺状态
        $status = DB::table('shops')->where('id','=',$id)->value('status');
        $newstatus = $request->all()['status'];
        //原来状态与现在提交状态不一样再更新
        if($status!==$newstatus){
            $aa['status'] = $newstatus;
            $res = DB::table('shops')->where('id','=',1)->update($aa);
            if(!$res){
                return back();
            }
        }
        
        if($request -> hasFile('logo')){
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('logo')->getClientOriginalExtension();
            //上传路径
            $dirname = './uploads/shop/';
            //文件名
            $name = $filename.'.'.$suffix;
            //上传头像
            $request->file('logo')->move($dirname,$name);
            //
            $data['logo'] = trim($dirname.$name,'.');
        }
        // dd($data);        
        $gid = DB::table('shops_details')->where('id','=',$id)->update($data);
        if($gid){
            return redirect('/shop/index');
        }else{
            return back();
        }    
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
